package com.frost.mybatis.mapper;

import com.frost.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 访问数据库
 */
@Mapper
public interface UserInfoMapper {
    /**
     * 返回用户数据列表
     * @return
     */
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> queryUserList();

    @Results(id = "BaseResult", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("select * from userinfo where id = #{id}")
    UserInfo queryUserInfo(Integer userId);

    @Select("select * from userinfo where name = ${name}")
    List<UserInfo> queryUserInfoByName2(String name);

    @ResultMap(value = "BaseResult")
    @Select("select * from userinfo where id = #{param1} and delete_flag = #{param2}")
    UserInfo queryUserInfoByDF(Integer userId, Integer deleteFlag);

    @Select("select * from userinfo where id = #{id} and delete_flag = #{deleteFlag}")
    UserInfo queryUserInfoParam(@Param("id") Integer userId, @Param("deleteFlag") Integer deleteFlag);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo(username, `password`, age, gender, phone) " +
            "values(#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(@Param("id") Integer id);

    @Update("update userinfo set password = #{password} where id = #{id}")
    Integer update(@Param("password") String password, @Param("id") Integer id);

    @Update("update userinfo set username = #{username}, password = #{password}, age = #{age} where id = #{id}")
    Integer updateByOb(UserInfo userInfo);

    @Select("select * from userinfo where name = '${name}' and password = '${password}'")
    List<UserInfo> queryUserInfoByName(String name, String password);


    @Delete("<script>" +
            "delete from userinfo where id in" +
            "<foreach collection='ids' separator=',' open='(' close=')' item='id'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    Integer batchDelete(List<Integer> ids);
}

